<template>
  <div>
    <p-modal
      title="Title"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <p-form-model
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >

        <p-form-model-item
          ref="coefficient"
          label="系数"
          prop="coefficient"
        >
          <p-input v-model="form.coefficient" />
        </p-form-model-item>

        <p-form-model-item ref="remark" label="备注" prop="remark">
          <p-input v-model="form.remark" />
        </p-form-model-item>
      </p-form-model>
    </p-modal>
  </div>
</template>
<script>
import { postAction, getAction, putAction, deleteAction } from "@/api/manage";

export default {
  data() {
    return {
      visible: false,
      confirmLoading: false,
      labelCol: { span: 8 },
      wrapperCol: { span: 12 },
      state: "add",
      form: {
        coefficient: "",
        remark: "",
      },
      rules: {
        coefficient: [
          { required: true, message: "请输入系数", trigger: "blur" },
        ],
      },
      proLineList: [],
      url: {
        add: "/mes-manpower/mesHrTotalRule",
        edit: "/mes-manpower/mesHrTotalRule",
      },
    };
  },
  mounted() {},
  methods: {
    showModal(record) {
      if (record) {
        this.state = "edit";
        for (let key in this.form) {
          this.form[key] = record[key];
        }
        this.editId = record.id;
      } else {
        this.state = "add";
      }

      this.visible = true;
    },
    handleOk(e) {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(this.form));
          params.type = 4
          this.confirmLoading = true;
          if (this.state == "add") {
            postAction(this.url.add, params).then((res) => {
              if (res.success) {
                this.handleCancel()
                this.$emit("handleSuccess");
              } else {
                this.$message.error(res.msg);
              }
              this.confirmLoading = false;
            });
          }

          if (this.state == "edit") {
            params.id = this.editId;
            putAction(this.url.edit, params).then((res) => {
              if (res.success) {
                this.handleCancel()
                this.$emit("handleSuccess");
              } else {
                this.$message.error(res.msg);
              }
              this.confirmLoading = false;
            });
          }
        }
      });
    },
    handleCancel(e) {
      this.visible = false;
      Object.assign(this.form, this.$options.data().form);
      this.$refs.ruleForm.resetFields()
    },
  },
};
</script>
